//
//  BzVC+PLAY.swift
//  Auscultation
//
//  Created by 来 on 2021/1/6.
//

import Foundation

// 播放器
extension BzVC: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !scrollView.isTracking && !scrollView.isDragging && !scrollView.isDecelerating {
            return
        }
        paused()
        let value = CGFloat(self.play_line.maximumValue) * CGFloat(scrollView.contentOffset.x) / CGFloat(scrollView.contentSize.width - scrollView.lgf_Width)
        player.seek(Float(value))
        play_time.text = player.getAllTime(Int(value) * 1000)
        play_line.setValue(Float(value), animated: false)
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !scrollView.isDragging {
            if isPlay {
                resume()
            }
        }
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if isPlay {
            resume()
        }
    }
    
    @objc func playNow(_ sender: UIButton) {
        if wavPath == nil {
            return
        }
        if wavPath.count == 0 {
            return
        }
        if player.state == .playing {
            isPlay = false
            paused()
        } else if player.state == .paused {
            resume()
        } else {
            play()
        }
    }
    
    func run(_ value: Float) {
        self.play_line!.value = value
        self.scroll(value)
        self.play_time.text = player.getTime()
    }
    
    func scroll(_ value: Float) {
        if self.play_line.maximumValue == 0 {
            return
        }
        let value: CGFloat = CGFloat(value) / CGFloat(self.play_line.maximumValue) * CGFloat(self.ph_view.contentSize.width - self.ph_view.lgf_Width)
        self.ph_view.setContentOffset(CGPoint.init(x: value, y: 0), animated: false)
    }
    
    func paused() {
        player.paused()
        ph_view.isScrollEnabled = true
        play_button.setBackgroundImage(#imageLiteral(resourceName: "bofang"), for: .normal)
    }
    
    func stop() {
        isPlay = false
        play_time.text = "/00:00"
        play_line.setValue(0, animated: false)
        ph_view.setContentOffset(CGPoint.zero, animated: false)
        player?.stop()
        play_button.setBackgroundImage(#imageLiteral(resourceName: "bofang"), for: .normal)
    }
    
    func resume() {
        isPlay = true
        player.resume()
        play_button.setBackgroundImage(#imageLiteral(resourceName: "zanting"), for: .normal)
    }
    
    func play() {
        isPlay = true
        if wavBdPath != nil {
            player.playFile(wavBdPath)
        } else {
            player.playOnLine(wavPath)
        }
        play_button.setBackgroundImage(#imageLiteral(resourceName: "zanting"), for: .normal)
    }

    @objc func s_end(_ sender: UISlider) {
        if isPlay {
            resume()
        }
    }
    @objc func s_changed(_ sender: UISlider) {
        player.seek(sender.value)
        play_time.text = player.getAllTime(Int(sender.value) * 1000)
        scroll(sender.value)
        paused()
    }
    @objc func s_tap(_ sender: UITapGestureRecognizer) {
        let touchPoint = sender.location(in: play_line)
        let value = Float(play_line.maximumValue - play_line.minimumValue) * Float(touchPoint.x / play_line.lgf_Width)
        scroll(value)
        play_line.setValue(value, animated: true)
        player.seek(value)
    }
    
}

extension BzVC: LGFAudioPlayerDelegate {
    func LGFAudioPlayerState(_ player: STKAudioPlayer) {
        self.play_line.maximumValue = Float(player.duration)
    }
    
    func LGFAudioPlayerRun(_ player: STKAudioPlayer) {
        self.run(Float(player.progress))
    }
    
    func LGFAudioPlayerStop(_ player: STKAudioPlayer) {
        self.stop()
    }
}

